<template>
    <div class="container">
      <el-dialog
        :title="form.id ? '修改学科' : '新增学科'"
        :visible.sync="dialogFormVisible"
        width="22%"
        @close="closeMyDia"
      >
        <el-form :model="form" ref="form" :rules="rules">
          <el-form-item prop="subjectID" label="所属学科" label-width="80px">
            <el-select
              v-model="form.subjectID"
              placeholder="请选择"
              :style="{ width: '100%' }"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in permissionGroupsList"
                :key="item.value"
              >
              </el-option>
              <el-option label="禁用" :value="0"> </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="目录名称" label-width="80px" prop="directoryName">
            <el-input
              v-model="form.directoryName"
              :style="{ width: '100%' }"
              placeholder="请输入目录名称"
            >
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

<script>
import { add, update } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  data () {
    return {
      dialogFormVisible: false,
      form: {
        directoryName: '',
        subjectID: '',
        id: null
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      },
      permissionGroupsList: []
    }
  },
  methods: {
    openMyDia (row) {
      this.dialogFormVisible = true
      if (row) {
        this.form.id = row.id
        this.form.directoryName = row.directoryName
        this.form.subjectID = row.subjectID
      }
    },
    closeMyDia () {
      this.$emit('init')
      this.form = {
        directoryName: '',
        subjectID: '',
        id: null
      }
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    async submit () {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          await update(this.form)
          this.$message.success('修改成功')
        } else {
          await add(this.form)
          this.$message.success('添加成功')
        }

        this.closeMyDia()
      } catch (error) {
        console.log(error)
      }
    }
  },
  async created () {
    const { data } = await simple()
    this.permissionGroupsList = data
  }
}
</script>

  <style scoped lang="less">
  .dialog-footer {
    display: flex;
    justify-content: end;
  }
  </style>
